package com.aegean.CommercialHotelBookSystem.admin.daoImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.aegean.CommercialHotelBookSystem.admin.dao.IAdminDao;
import com.aegean.CommercialHotelBookSystem.factory.HibernateSessionFactory;
import com.aegean.CommercialHotelBookSystem.factory.MyTools;
import com.aegean.CommercialHotelBookSystem.model.Book;
import com.aegean.CommercialHotelBookSystem.model.BookDetail;
import com.aegean.CommercialHotelBookSystem.model.Customer;
import com.aegean.CommercialHotelBookSystem.model.Room;
import com.aegean.CommercialHotelBookSystem.model.Service;
import com.aegean.CommercialHotelBookSystem.user.dao.IUserDao;

public class AdminDaoImpl implements IAdminDao {

	@Override
	public Service updateObject(Service one) {
		Session session = null;
		Transaction tx = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			if (one instanceof Service) {
				session.update(one);
				tx.commit();
			}
			return one;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return null;
		} finally {
			closeSession();
		}
	}

	@Override
	public Session getSession() {
		return HibernateSessionFactory.getSession();
	}

	@Override
	public void closeSession() {
		HibernateSessionFactory.closeSession();
	}

	@Override
	public Service saveObject(Object o) {
		Session session = null;
		Transaction tx = null;
		Service one = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			if (o instanceof Service) {
				one = (Service)o;
				session.save(one);
				tx.commit();
			}
			return one;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return null;
		} finally {
			closeSession();
		}
	}

	@Override
	public Service searchService(String Service_Name, String Service_Pwd) {
		Session session = null;
		Transaction tx = null;
		Service one = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			Query q = session
					.createQuery("from Service where person_Login=:name and person_Password=:pwd");
			q.setString("name", Service_Name);
			q.setString("pwd", Service_Pwd);
			one = (Service) q.uniqueResult();
			return one;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return null;
		} finally {
			closeSession();
		}
	}

	@Override
	public Service updatePwd(Service one) {
		Session session = null;
		Transaction tx = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			if (one instanceof Service) {
				session.merge(one);
				tx.commit();
			}
			return one;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return null;
		} finally {
			closeSession();
		}
	}

	@Override
	public boolean searchAdmin(String No) {
		Session session = null;
		Transaction tx = null;
		Service one;
		Query q;
		int service_No=Integer.parseInt(No);
		try {
			session = getSession();
			tx = session.beginTransaction();
			q = session.createQuery("from Service where service_No=:no");
			q.setInteger("no",service_No);
			one = (Service) q.uniqueResult();
			if (one == null) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return false;
		} finally {
			closeSession();
		}
	}

}
